import BaseUtil from '@/BaseUtil';
import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import BuffCommon from 'code/Core/Common/BuffCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 奥术传送
 **/
export default class FS_ArcanTransfer extends AbilityBase {
    static instance: FS_ArcanTransfer;
    buffType: AppBuffType = {
        id: 'buff_FS_ArcanTransfer',
        art: this.art,
        name: this.abilityName,
        ubertip: '正在传送',
        class: '光环',
        dur: 1,
        targetAttach: 'origin',
        targetArt: 'Abilities\\Spells\\Human\\MassTeleport\\MassTeleportCaster.mdl',
    };
    constructor(id: string) {
        super(id);
        FS_ArcanTransfer.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let us = SelectCommon.getAllyUnitInRange(source, 350);
        for (let u of us) {
            // BuffUtil.addBuff(u, this.buffType.id, source);
            BuffCommon.addBuff({ target: u, caster: source, buffTypeId: this.buffType.id, ablityId: this.id });
        }
        let lv = this.getAbilityLevel(source);
        BaseUtil.onTimer(0.5, (c) => {
            let cmd = GetUnitCurrentOrder(source);
            if (cmd != OrderId.Spellsteal) return false;
            let us = SelectCommon.getAllyUnitInRange(source, 350);
            for (let u of us) {
                // BuffUtil.addBuff(u, this.buffType.id, source);
                BuffCommon.addBuff({ target: u, caster: source, buffTypeId: this.buffType.id, ablityId: this.id });
            }
            if (c == 6) {
                let us = SelectCommon.getAllyUnitInRangeExcludeCenterUnit(source, 350);
                for (let u of us) {
                    let sx = GetUnitX(source);
                    let sy = GetUnitY(source);
                    let ux = GetUnitX(u);
                    let uy = GetUnitY(u);
                    let ag = MathUtil.angleBetweenCoords(sx, sy, ux, uy);
                    let dis = MathUtil.distanceBetweenPoints(sx, sy, ux, uy);
                    let { x, y } = MathUtil.polarProjection(tarX, tarY, dis, ag);
                    SetUnitX(u, x);
                    SetUnitY(u, y);
                    BuffUtil.clearUnitBuff(u, this.buffType.id, source);
                    DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\Human\\MassTeleport\\MassTeleportTarget.mdl', u, 'origin'));
                }
                SetUnitX(source, tarX);
                SetUnitY(source, tarY);
                BuffUtil.clearUnitBuff(source, this.buffType.id, source);
                DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\Human\\MassTeleport\\MassTeleportTarget.mdl', source, 'origin'));

                let es = SelectUtil.getEnemyUnitsInRange(source, 350, tarX, tarY);
                let dmg = 10 * lv + UnitCommon.取法强(source) * 0.3 + 0.2 * lv;
                for (let e of es) {
                    let angle = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), GetUnitX(e), GetUnitY(e));
                    MovementCommon.hitFly({
                        target: e,
                        caster: source,
                        distance: 350,
                        time: 0.5,
                        angle,
                        maxHeight: 250,
                    });
                    DamageSystemInstance.applyArcaneDamage(e, source, dmg, this.id);
                }
            }
            return c < 6;
        });

        BaseUtil.onTimer(0.1, (c) => {
            let cmd = GetUnitCurrentOrder(source);
            if (cmd != OrderId.Spellsteal) return false;
            let es = SelectUtil.getEnemyUnitsInRange(source, 350);
            let dmg = 10 * lv + UnitCommon.取法强(source) * 0.3 + 0.2 * lv;
            for (let e of es) {
                let angle = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), GetUnitX(e), GetUnitY(e));
                MovementCommon.hitFly({
                    target: e,
                    caster: source,
                    distance: 350,
                    time: 0.5,
                    angle,
                    maxHeight: 250,
                });
                DamageSystemInstance.applyArcaneDamage(e, source, dmg, this.id);
            }
            return c < 30;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
